<?php

/**
 * game actions.
 *
 * @package    dylemat
 * @subpackage game
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class gameActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
	
  }
  
  public function executeStartPalying(sfWebRequest $request)
  {
	
  }
  
  public function executeCheckRound(sfWebRequest $request)
  {
      //return $this->renderText('1');
      $oTournament=TournamentPeer::retrieveByPK($request->getParameter('id'));
      if($oTournament->getStatus()!=Tournament::$GAME_STATUS_ENDED){
          $this->oActivPlayer = $this->getUser()->getActivPlayer();
           $c = new Criteria();
           $c->add(GameMatchPeer::STATUS, GameMatch::$GAMEMATCH_STATUS_START);
           $c->add(GameMatchPeer::TOURNAMENT_ID,$oTournament->getId());
           $criterion1 = $c->getNewCriterion(GameMatchPeer::FIRST_PLAYER_ID, $this->oActivPlayer->getId(), Criteria::EQUAL);
           $criterion2 = $c->getNewCriterion(GameMatchPeer::SECOND_PLAYER_ID, $this->oActivPlayer->getId(), Criteria::EQUAL);
           $criterion1->addOr($criterion2);
           $c->add($criterion1);
           $c->addDescendingOrderByColumn(GameMatchPeer::ID);
           
            if(!GameMatchPeer::doSelectOne($c))
            {
                return $this->renderText('0');
            }else{          
                return $this->renderText('1');
            }
          
      }else{
          return $this->renderText('2');
      }
      
  }
  
  public function executeCopyTournament(sfWebRequest $request)
  {
      $oTournament=TournamentPeer::retrieveByPK($request->getParameter('id'));
      $oNewTournament=new Tournament();
      $oNewTournament->setAckRound(0);
      $oNewTournament->setStatus(Tournament::$GAME_STATUS_IDLE);
      $oNewTournament->setName(substr(sha1(time()),0,10));
      $oNewTournament->setType($oTournament->getType());
      $oNewTournament->setRuleId($oTournament->getRuleId());
      $oNewTournament->setRound($oTournament->getRound());
      $oNewTournament->save();
      $this->setTemplate('deleteTournament');
  }
  
  public function executeDBEditTournament(sfWebRequest $request)
  {
        $this->form=new TournamentForm();
        if ($request->isMethod('post'))
        {            
            $param=$request->getParameter('newtur');
            $oTournament=TournamentPeer::retrieveByPK($param['id']);
            $this->form->bind($request->getParameter('newtur'));
            if($oTournament)
            {
               $oTournament->setName($param['name']);
               $oTournament->setRound($param['round']);
               $oTournament->setRuleId($param['rule_id']);
               $oTournament->setType($param['type']);
               $oTournament->save();
               $this->redirect('@mod_tournament');
            }
            /*
              
             
            if ($this->form->isValid())
            {
                $this->form->save();
                $this->redirect('@mod_tournament');
            }*/
        }
        $this->setTemplate('addDbTournament');
  }
  
  public function executeEditTournament(sfWebRequest $request)
  {
	if ($request->isMethod('post'))
        {
            $this->form=new TournamentForm();
            $this->form->bind($request->getParameter('newtur'));
            if ($this->form->isValid())
            {
                $this->form->save();
                $this->redirect('@mod_tournament');
            }
        }else{	
		$c = new Criteria();
		$c->add(tournamentPeer::ID,$request->getParameter('id'));
		$v= tournamentPeer::doSelectOne($c);		
		$this->form=new TournamentForm($v);		
	}
	$this->setTemplate('addDbTournament');
  }
  
  public function executeAddDbTournament(sfWebRequest $request)
  {
	$this->form=new TournamentForm();
	
	if ($request->isMethod('post'))
        {
            $this->form->bind($request->getParameter('newtur'));
            if ($this->form->isValid())
            {
                $this->form->save();
                $this->redirect('@mod_tournament');
            }else{
                
            }
        }
	
  }
  
  public function executeTournamentSignIn(sfWebRequest $request)
  {
	$ac=new activPlayer();
	$ac->setUserId($this->getUser()->getUserId());
	$ac->setTournamentId($request->getParameter('id'));
	$ac->save();
	$this->redirect('@user_game');
  }
  
  public function executeStatus(sfWebRequest $request)
  {
      return $this->renderText(
			json_encode(
				array(
					'status'=>1,
					'init'=>array(
						'player_name'=>'zenon'
					)					
				)
			)
	);	
  }

  public function executeTournament(sfWebRequest $request)
  {
    $this->forward404Unless($this->getUser()->isPremium());
	$this->page = ($request->getParameter('page') == null) ? 1 : $request->getParameter('page');
  }
  
  public function executeShowTournament(sfWebRequest $request) {
        $c = new Criteria();
        $c->add(TournamentPeer::ID, $request->getParameter('id'));
        $this->tour = TournamentPeer::doSelectOne($c);
        $this->error = null;

        if ($this->tour)
        {
            $this->tour_id = $this->tour->getId();
            /*
            $c = new Criteria();
            $c->add(GameMatchPeer::TOURNAMENT_ID, $this->tour_id);
            $c->add(GameMatchPeer::STATUS, GameMatch::$GAMEMATCH_STATUS_ENDED);
            $result = GameMatchPeer::doSelect($c);

            $all_match = array();
            
            foreach ($result as $res)
            {
               $all_match[] = $res->getId();
            }
                    
            $c = new Criteria();
            $c->addSelectColumn(ScorePeer::PLAYER_ID);
            $c->addSelectColumn('SUM(' . ScorePeer::BIG_SCORE . ') as cnt');
            $c->add(ScorePeer::MATCH_ID, $all_match, Criteria::IN);
            $c->addGroupByColumn(ScorePeer::PLAYER_ID);
            $res = ScorePeer::doSelectStmt($c);
            
            */
            
            $c = new Criteria();
            $c->add(ActivPlayerPeer::TOURNAMENT_ID, $this->tour->getId());
            $this->active = ActivPlayerPeer::doSelect($c);
            $this->rules = $this->tour->getRules();
            $this->ruleData = unserialize($this->rules->getData());
            
            if ($this->active) {

                if (!$this->matches) {


                    //ranking

                    $c = new Criteria();
                    $c->add(GameMatchPeer::TOURNAMENT_ID, $this->tour_id);
                    $c->add(GameMatchPeer::STATUS, GameMatch::$GAMEMATCH_STATUS_ENDED);
                    $result = GameMatchPeer::doSelect($c);

                    $all_match = array();

                    foreach ($result as $res) {
                        $all_match[] = $res->getId();
                    }

                    $c = new Criteria();
                    $c->addSelectColumn(ScorePeer::PLAYER_ID);
                    $c->addSelectColumn('SUM(' . ScorePeer::BIG_SCORE . ') as cnt');
                    $c->add(ScorePeer::MATCH_ID, $all_match, Criteria::IN);
                    $c->addGroupByColumn(ScorePeer::PLAYER_ID);

                    $mt = unserialize($this->tour->getRules()->getData());

                    if ($mt->getWinPoints() == gameRules::$GW_Less)
                        $c->addAscendingOrderByColumn('cnt');
                    else
                        $c->addDescendingOrderByColumn('cnt');

                    $res = ScorePeer::doSelectStmt($c);

                    //cd nizej
                    //pobieranie danych meczu
                    $c = new Criteria();
                    $c->addSelectColumn(GameMatchPeer::ID);
                    $c->addSelectColumn(GameMatchPeer::ROUND_ID);
                    $c->addSelectColumn(GameMatchPeer::FIRST_PLAYER_ID);
                    $c->addSelectColumn(GameMatchPeer::SECOND_PLAYER_ID);
                    $c->addSelectColumn(GameMatchPeer::RESULT);
                    $c->addSelectColumn(GameMatchPeer::STATUS);
                    $c->addSelectColumn(GameMatchPeer::CREATED_AT);
                    $c->add(GameMatchPeer::TOURNAMENT_ID, $this->tour_id);
                    //$c->add(GameMatchPeer::STATUS, GameMatch::$GAMEMATCH_STATUS_ENDED);
                    $c->addAscendingOrderByColumn(GameMatchPeer::ROUND_ID);
                    $c->addDescendingOrderByColumn(GameMatchPeer::CREATED_AT);
                    $this->matches = GameMatchPeer::doSelectStmt($c);

                    $match_ids = array();
                    $this->aMatches = array();
                    foreach ($this->matches->fetchAll() as $m) {
                        $match_ids[] = $m[0];
                        $this->aMatches[$m[0]] = array($m[1], $m[2], $m[3], $m[4], $m[5],'', '', 0, 0,0,$m[0]); // id | round activfirst activsecond result | nick1 nick2 score1 score2
                    }

                    foreach ($this->matches->fetchAll() as $m) {
                        
                    }

                    //pobieranie nicków activ_playerów
                    $c = new Criteria();
                    $c->addSelectColumn(ActivPlayerPeer::ID);
                    $c->addSelectColumn(PlayerPeer::NICK);
                    $c->addJoin(ActivPlayerPeer::USER_ID, PlayerPeer::ID);
                    $c->add(ActivPlayerPeer::TOURNAMENT_ID, $this->tour_id);
                    $c->addAscendingOrderByColumn(ActivPlayerPeer::ID);
                    $this->nicks = ScorePeer::doSelectStmt($c);

                    //pobieranie sumy punktów
                    $c = new Criteria();
                    $c->addSelectColumn(ScorePeer::MATCH_ID);
                    $c->addSelectColumn(ScorePeer::PLAYER_ID);
                    $c->addSelectColumn('SUM(' . ScorePeer::BIG_SCORE . ') as sum');
                    $c->add(ScorePeer::MATCH_ID, $match_ids, Criteria::IN);
                    $c->addGroupByColumn(ScorePeer::MATCH_ID);
                    $c->addGroupByColumn(ScorePeer::PLAYER_ID);
                    $c->addAscendingOrderByColumn(ScorePeer::MATCH_ID);
                    $c->addAscendingOrderByColumn(ScorePeer::PLAYER_ID);
                    $this->scores = ScorePeer::doSelectStmt($c);

                    $this->aScores = array();
                    foreach ($this->scores->fetchAll() as $s) {
                        $this->aScores[] = array($s[0], $s[1], $s[2]); //match player sum
                    }

                    $this->aNicks = array();
                    foreach ($this->nicks->fetchAll() as $n) {
                        $this->aNicks[$n[0]] = $n[1]; //index to activ id a value to nick
                    }

                    //ranking

                    $this->ranking = array();

                    foreach ($res->fetchAll() as $node) {
                        $this->ranking[] = array($this->aNicks[$node[0]], $node[1]);
                    }

                    ///ranking
                    foreach ($this->aMatches as $key => $value) {
                        $this->aMatches[$key][5] = $this->aNicks[$value[1]];
                        $this->aMatches[$key][6] = $this->aNicks[$value[2]];

                        foreach ($this->aScores as $s) {
                            if ($key == $s[0] && $value[1] == $s[1]) {
                                $this->aMatches[$key][7] = $s[2];
                            }
                            if ($key == $s[0] && $value[2] == $s[1]) {
                                $this->aMatches[$key][8] = $s[2];
                            }
                        }

                        if ($this->aMatches[$key][4] == GameMatch::$GAMEMATCH_STATUS_ENDED) {
                            switch ($this->aMatches[$key][3]) {
                                case GameMatch::$GAMEMATCH_RESULT_DRAW:
                                    $this->aMatches[$key][3] = "remis";
                                    break;
                                case GameMatch::$GAMEMATCH_RESULT_FIRST:
                                    $this->aMatches[$key][3] = "wygrał " . $this->aMatches[$key][5];
                                    break;
                                case GameMatch::$GAMEMATCH_RESULT_SECOND:
                                    $this->aMatches[$key][3] = "wygrał " . $this->aMatches[$key][6];
                                    break;
                            }
                        } else {
                            $this->aMatches[$key][9] = 1;
                            $this->aMatches[$key][3] = "w trakcie gry";
                        }
                    }
                } else {
                    $this->error = "Brak danych meczów";
                    return;
                }
            } else {
                $this->error = "Brak graczy.";
                return;
            }
        } else {
            $this->error = "Turniej nie istnieje.";
            return;
        }
    }
	
  public function executeSendOptionTree(sfWebRequest $request)
  {
      $text='';
      
      $oActivPlayer=$this->getUser()->getActivPlayer();
      
      $c = new Criteria();
      $c->add(GameMatchPeer::STATUS,  GameMatch::$GAMEMATCH_STATUS_START);
      $criterion1 =   $c -> getNewCriterion( GameMatchPeer::FIRST_PLAYER_ID, $oActivPlayer->getId(), Criteria::EQUAL );  
      $criterion2 =   $c -> getNewCriterion( GameMatchPeer::SECOND_PLAYER_ID,$oActivPlayer->getId(), Criteria::EQUAL );  
      $criterion1->addOr($criterion2);
      $c->add($criterion1);
      $c->addDescendingOrderByColumn(GameMatchPeer::CREATED_AT);
      $oActMatch=GameMatchPeer::doSelectOne($c);
      
      $c_com_co = new Criteria();
      $c_com_co->add(gamePeer::MATCH_ID,$oActMatch->getId());
      $c_com_co->add(gamePeer::STATUS,game::$GAME_STATUS_ENDED,Criteria::NOT_EQUAL);
      $c_com_co->addDescendingOrderByColumn(gamePeer::CREATED_AT);
      $game= gamePeer::doSelectOne($c_com_co);
      
      $tree=  unserialize($this->getUser()->getTournament()->getRules()->getData());
      
      $node=$tree->getNode($game->getNode());
      
      
      $mode_action=$node->getActions();
      
      $next_node_id=$mode_action[$request->getParameter('id')-1][1];
      $node=$tree->getNode($next_node_id);
      
      while($node->getType()==gameRules::$TN_NATURE)
      {         
          $mode_action=$node->getActions();
          $next_node_id=$mode_action[$node->getRandomId()][2];
          $node=$tree->getNode($next_node_id);
      }
      
      if($node->getType()==gameRules::$TN_POINTS)
      {                       
        $text='Koniec gry. ';
        //$iGameCount = GamePeer::getGameCount($oActMatch->getId());
        //$iMatchCount = GameMatchPeer::getMatchCount($oActMatch->getTournamentId());                        

        //$iFirstplayer=$this->getUser()->isFirstGamePlayer($matrix,$iMatchCount,$iGameCount,$oActMatch->getFirstPlayerId()==$oActivPlayer->getId());
        $iFirstplayer=$oActMatch->getFirstPlayerId()==$oActivPlayer->getId();
        if($iFirstplayer)
        {
            $score = new Score();
            $score->setGameId($game->getId());
            $score->setMatchId($game->getMatchId());
            $score->setPlayerId($oActMatch->getFirstPlayerId());
            $score->setOption(0);
            $score->setScore($node->getPlayerPoints($iFirstplayer));
            //$score->save();

            $score2 = new Score();
            $score2->setGameId($game->getId());
            $score2->setMatchId($game->getMatchId());
            $score2->setPlayerId($oActMatch->getSecondPlayerId());
            $score2->setOption(0);
            $score2->setScore($node->getPlayerPoints(!$iFirstplayer));
            //$score2->save();
            
            
             if($tree->getWinPoints() == gameRules::$GW_Less)
             {
                    if($node->getPlayerPoints($iFirstplayer)<$node->getPlayerPoints(!$iFirstplayer)){
                        $score2->setBigScore(1.0);
                         $text='Wygrałeś!!!';
                    }else if($node->getPlayerPoints($iFirstplayer)==$node->getPlayerPoints(!$iFirstplayer)){
                        $score->setBigScore(1.0);
                        $score2->setBigScore(0.5);
                        $text='Remis'; 
                    }else{
                         $score->setBigScore(1.0);
                          $text='Przegrałeś!'; 
                    }
                }else{
                    if($node->getPlayerPoints($iFirstplayer)>$node->getPlayerPoints(!$iFirstplayer))
                    {
                        $score->setBigScore(1.0);
                         $text='Wygrałeś!!!';
                    }else if($node->getPlayerPoints($iFirstplayer)==$node->getPlayerPoints(!$iFirstplayer)){
                        $score->setBigScore(1.0);
                        $score2->setBigScore(0.5);
                        $text='Remis'; 
                    }else{
                        $score2->setBigScore(1.0);
                         $text='Przegrałeś!'; 
                    }
                }
            
            
            $score->save();
            $score2->save();
            
        }else{
            $score = new Score();
            $score->setGameId($game->getId());
            $score->setMatchId($game->getMatchId());
            $score->setPlayerId($oActMatch->getFirstPlayerId());
            $score->setOption(0);
            $score->setScore($node->getPlayerPoints(!$iFirstplayer));
            //$score->save();

            $score2 = new Score();
            $score2->setGameId($game->getId());
            $score2->setMatchId($game->getMatchId());
            $score2->setPlayerId($oActMatch->getSecondPlayerId());
            $score2->setOption(0);
            $score2->setScore($node->getPlayerPoints($iFirstplayer));
           // $score->save();
            
             if($tree->getWinPoints() == gameRules::$GW_Less)
             {
                    if($node->getPlayerPoints($iFirstplayer)<$node->getPlayerPoints(!$iFirstplayer)){
                        $score2->setBigScore(1.0);
                         $text='Przegrałeś!'; 
                    }else if($node->getPlayerPoints($iFirstplayer)==$node->getPlayerPoints(!$iFirstplayer)){
                        $score->setBigScore(1.0);
                        $score2->setBigScore(0.5);
                        $text='Remis'; 
                    }else{
                         $score->setBigScore(1.0);
                         $text='Wygrałeś!!!';
                    }
                }else{
                    if($node->getPlayerPoints($iFirstplayer)>$node->getPlayerPoints(!$iFirstplayer))
                    {
                        $score->setBigScore(1.0);
                         $text='Przegrałeś!'; 
                    }else if($node->getPlayerPoints($iFirstplayer)==$node->getPlayerPoints(!$iFirstplayer)){
                        $score->setBigScore(1.0);
                        $score2->setBigScore(0.5);
                        $text='Remis'; 
                    }else{
                        $score2->setBigScore(1.0);
                        $text='Wygrałeś!!!';
                    }
                }
            
            
            $score->save();
            $score2->save();
            
        }
        
      }else{
          //$text='Koniec gry';
      }
      
      $game->setNode($node->getId());
      $game->save();
                  
      return $this->renderText($text);
  }
  public function executeSendOption(sfWebRequest $request)
  {
      $text='';
      
        $oActivPlayer=$this->getUser()->getActivPlayer();
            
        $c = new Criteria();
        $c->add(GameMatchPeer::STATUS,  GameMatch::$GAMEMATCH_STATUS_START);
        $c->add(GameMatchPeer::TOURNAMENT_ID,$oActivPlayer->getTournamentId());
        $criterion1 =   $c -> getNewCriterion( GameMatchPeer::FIRST_PLAYER_ID, $oActivPlayer->getId(), Criteria::EQUAL );  
        $criterion2 =   $c -> getNewCriterion( GameMatchPeer::SECOND_PLAYER_ID,$oActivPlayer->getId(), Criteria::EQUAL );  
        $criterion1->addOr($criterion2);
        $c->add($criterion1);
        $c->addDescendingOrderByColumn(GameMatchPeer::CREATED_AT);
        $oActMatch=GameMatchPeer::doSelectOne($c);
      
        $c_com_co = new Criteria();
        $c_com_co->add(gamePeer::MATCH_ID,$oActMatch->getId());
        $c_com_co->add(gamePeer::STATUS,game::$GAME_STATUS_ENDED,Criteria::NOT_EQUAL);
        $c_com_co->addAscendingOrderByColumn(gamePeer::CREATED_AT);
        $game= gamePeer::doSelectOne($c_com_co);
      
        if ($game)
        {
            $game_scores = ScorePeer::retrieveByGame($game->getId());
            $score_count=count($game_scores);
            
            
            if ($score_count < 2) 
            {
                $add=true;
                
                $tmp_score=0;
                $tmp_option=$request->getParameter('id')-1;
                $tmp_big_score=0;
                if($score_count)
                {
                    if ($game_scores[0]->getPlayerId() == $oActivPlayer->getId()) {
                        $add=false;
                    }else{
                        $oRules=$oActivPlayer->getTournament()->getRules();
                        $matrix=  unserialize($oRules->getData());
                        $points=null;
                        
                        $iFirstplayer=$oActMatch->getFirstPlayerId()==$oActivPlayer->getId();
                        
                        if($iFirstplayer)
                        {
                            $points=$matrix->getCell($tmp_option,$game_scores[0]->getOption()); 
                        }else{
                            $points=$matrix->getCell($game_scores[0]->getOption(),$tmp_option);
                        }
                        
                        $tmp_score=$points[$iFirstplayer];
                                                
                        
                        if($matrix->getWinPoints() == gameRules::$GW_Less)
                        {
                            if($tmp_score<$points[!$iFirstplayer]){
                                $tmp_big_score=1.0;
                                $text=($iFirstplayer)?'Wygrałeś!!!':'Przegrałeś!';
                            }else if($tmp_score==$points[!$iFirstplayer]){
                                $tmp_big_score=0.5;
                                $game_scores[0]->setBigScore(0.5);
                                $text='Remis';
                            }else{
                                $game_scores[0]->setBigScore(1);
                                $text=($iFirstplayer)?'Wygrałeś!!!':'Przegrałeś!';
                            }
                        }else{
                            if($tmp_score>$points[!$iFirstplayer]){
                                $tmp_big_score=1.0;
                                $text=(!$iFirstplayer)?'Wygrałeś!!!':'Przegrałeś!';
                            }else if($tmp_score==$points[!$iFirstplayer]){
                                $tmp_big_score=0.5;
                                $game_scores[0]->setBigScore(0.5);
                                $text='Remis';
                            }else{
                                $game_scores[0]->setBigScore(1);
                                $text=(!$iFirstplayer)?'Wygrałeś!!!':'Przegrałeś!';
                            }
                        }
                        
                        
                        $game_scores[0]->setScore($points[!$iFirstplayer]);
                        $game_scores[0]->save();
                        
                       /* 
                        $iGameCount = GamePeer::getGameCount($oActMatch->getId());
                        $iMatchCount = GameMatchPeer::getMatchCount($oActMatch->getTournamentId());                        
                        
                        $iFirstplayer=$this->getUser()->isFirstGamePlayer($matrix,$iMatchCount,$iGameCount,$oActMatch->getFirstPlayerId()==$oActivPlayer->getId());
                        
                        if(!$iFirstplayer)
                        {    
                            $points=$matrix->getCell($tmp_option,$game_scores[0]->getOption());
                        }else{
                            $points=$matrix->getCell($game_scores[0]->getOption(),$tmp_option);
                        }
                        */
                        
                    }
                    
                }
                
                if($add)
                {
                    $score = new Score();
                    $score->setGameId($game->getId());
                    $score->setMatchId($game->getMatchId());
                    $score->setPlayerId($this->getUser()->getActivPlayerId());
                    $score->setOption($tmp_option);
                    $score->setBigScore($tmp_big_score);
                    $score->setScore($tmp_score);
                    $score->save();
                }
                    
                
            }
        }
	return $this->renderText($text);
  }
  
  public function executeGetPartner(sfWebRequest $request)
  {
    return $this->renderText(json_encode(array('match'=>array('act_score1'=>2,'act_score2'=>3,'all_score1'=>-12,'all_score2'=>-3))));
	//return $this->renderText('kolo');//$this->renderPartial(array('user','gameProfile'));
  }
  
  public function executeActivateTournament(sfWebRequest $request)
  {
    $this->forward404Unless($this->getUser()->isPremium());
	$oTournamet=tournamentPeer::retrieveByPK($request->getParameter('id'));
	$oTournamet->setStatus(tournament::$GAME_STATUS_START);
	$oTournamet->save();
        $this->redirect('@mod_tournament');
  }

  public function executePlayTournament(sfWebRequest $request)
  {
    $this->forward404Unless($this->getUser()->isPremium());
	$oTournamet=tournamentPeer::retrieveByPK($request->getParameter('id'));
	$oTournamet->setStatus(tournament::$GAME_STATUS_PLAYING);
	$oTournamet->save();
        $this->redirect('@mod_tournament');
  }  
  
  public function executeDeleteTournament(sfWebRequest $request)
  {
    $this->forward404Unless($this->getUser()->isPremium());
	
        $c = new Criteria();
        $c->add(GameMatchPeer::TOURNAMENT_ID,$request->getParameter('id'));
        GameMatchPeer::doDelete($c);
    
	$c = new Criteria();
	$c->add(tournamentPeer::ID, $request->getParameter('id'));
	tournamentPeer::doDelete($c);
  }  
   public function executeActivDelete(sfWebRequest $request) {
        $this->forward404Unless($this->getUser()->isPremium());

        $c = new Criteria();
        $c->add(ActivPlayerPeer::ID, $request->getParameter('id'));
        ActivPlayerPeer::doDelete($c);
        $this->getUser()->setFlash('notice', 'Usunięto gracza');
        $this->redirect('@show_tournament?id='.$request->getParameter('t'));
    }
    

    public function executeTerminateTournament(sfWebRequest $request) {
        $this->forward404Unless($this->getUser()->isPremium());

        
        
        $c = new Criteria();
        $c->add(ActivPlayerPeer::TOURNAMENT_ID, $request->getParameter('id'));
        $result=  ActivPlayerPeer::doSelect($c);
        
        foreach($result as $res){
            
            $res->setStatus(ActivPlayer::$ACTION_END_TURNAMENT);
            $res->save();
        }
        
        $oTournamet = tournamentPeer::retrieveByPK($request->getParameter('id'));
        $oTournamet->setStatus(tournament::$GAME_STATUS_ENDED);
        $oTournamet->save();
        $this->redirect('@mod_tournament');
    }
}
